package com.tierstall.client.presenter;

import com.google.gwt.event.shared.HandlerManager;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;

import com.tierstall.client.service.WebServices;

import com.tierstall.client.view.content.CV_addGroup;
import com.tierstall.client.view.content.CV_dbFilter;
import com.tierstall.shared.GruppeDTO;
import com.tierstall.shared.Page;
import com.tierstall.shared.UserDTO;

public class GroupMGMTPresenter implements Presenter,
		CV_addGroup.Display {

	private final WebServices rpcService;
	private final HandlerManager eventBus;
	private final CV_addGroup view;
	private final MainPresenter mPresenter;
	private final String sessionId;

	// private boolean isUpdating = false;

	public GroupMGMTPresenter(WebServices rpcServices, String session_id,
			HandlerManager eventBus, MainPresenter mPresenter, UserDTO currentUser) {
		this.rpcService = rpcServices;
		this.eventBus = eventBus;
		this.sessionId = session_id;
		this.mPresenter = mPresenter;
		this.view = (CV_addGroup) mPresenter.getContentView(Page.ADD_GROUP);
		this.view.setPresenter(this);
	}

	public void go(final HasWidgets container) {
		mPresenter.setContentView(view);
//		hView.setContent(view);
		// container.clear();
		// container.add(view.asWidget());
	}

	@Override
	public void onAddClicked(String genehmigung, String leiter) {
		GruppeDTO group = new GruppeDTO(null, genehmigung, leiter, 2, null, null, "no");
		rpcService.getRpcTierService().addGroup(sessionId, group, new AsyncCallback<GruppeDTO>() {

			@Override
			public void onSuccess(GruppeDTO result) {
				// TODO Auto-generated method stub
				view.succeeded();
			}
			
			@Override
			public void onFailure(Throwable caught) {
				view.failed();
				
			}

				});

	}

	// @Override
	// public void onLoginClicked(String username, String password, final
	// boolean save_session) {
	// rpcService.loginRequest(username, password, new AsyncCallback<String>() {
	// @Override
	// public void onSuccess(String session) {
	// if (session.equals("not existing")){
	// view.enableUsernameInfo("Falscher Benutzername!");
	// } else if (session.equals("wrong password")){
	// view.enablePasswordInfo("Falsches Passwort!");
	// } else { // pw + username correct
	// System.out.println("Session on Client-Side: " + session);
	// eventBus.fireEvent(new LoginEvent(save_session, session));
	// }
	//
	// }
	// public void onFailure(Throwable caught) {
	// Window.alert("Error retrieving contact");
	// }
	// });
	// }

}
